package fractal;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import entity.Position;

public class TerrainView extends BasicGame
{

	protected Position _pos;
	protected static int NBSCREEN = 128;
	protected static int TAILLECASE = 4;
	public static int DEPLACEMENT = 4;
	protected TerrainMap _Map;
	protected List<Color> _colors = null;
	protected ModeVisu _mode = null;
	protected GenerateurKingdom _GenerateurKingdom;

	public TerrainView(String title)
	{
		super(title);
	}

	@Override
	public void render( GameContainer arg0 , Graphics arg1 ) throws SlickException
	{
		// TODO Auto-generated method stub
		for (int i = _pos.getX(); i < _pos.getX() + NBSCREEN; i++)
		{
			for (int j = _pos.getY(); j < _pos.getY() + NBSCREEN; j++)
			{
				arg1.setColor(new Color(0, 0, 0));
				if (ModeVisu.TEMPERATURE.equals(_mode))
				{
					visuTemp(arg1, i, j);
				}
				if (ModeVisu.RELIEF.equals(_mode))
				{
					visuHeur(arg1, i, j);
				}
				if (ModeVisu.ALL.equals(_mode))
				{
					visuAll(arg1, i, j);
				}
				if (ModeVisu.KINGDOM.equals(_mode))
				{
					visuKingdom(arg1, i, j);
				}

				/*
				 * arg1.setColor(new Color(210, 197, 87)); if (_Map.getMap()[i][j].getEmpire() != -1 && _Map.getMap()[i][j].getEmpire() < _colors.size()) { arg1.setColor(_colors.get(_Map.getMap()[i][j].getEmpire())); }
				 */
				int x = (i - _pos.getX()) * TAILLECASE;
				int y = (j - _pos.getY()) * TAILLECASE;
				Rectangle rectangle = new Rectangle(x, y, x + TAILLECASE, y + TAILLECASE);
				arg1.fill(rectangle);
			}
		}
	}

	protected void visuAll( Graphics g , int x , int y )
	{
		TerrainType terrrainType = (_Map.getMap()[x][y]).getTerrainType();
		if (TerrainType.MOUNTAIN.equals(terrrainType))
		{
			g.setColor(new Color(227, 131, 18));
		}
		if (TerrainType.PLAT.equals(terrrainType))
		{
			g.setColor(new Color(177, 253, 69));
		}
		if (TerrainType.CITY.equals(terrrainType))
		{
			g.setColor(new Color(0, 0, 0));
		}
		if (TerrainType.SNOW.equals(terrrainType))
		{
			g.setColor(new Color(255, 255, 255));
		}
		if (TerrainType.DESERT.equals(terrrainType))
		{
			g.setColor(new Color(239, 212, 86));
		}
		if (TerrainType.WATER.equals(terrrainType))
		{
			g.setColor(new Color(166, 202, 240));
		}
		if (TerrainType.FOREST.equals(terrrainType))
		{
			g.setColor(new Color(10, 116, 10));
		}
	}

	protected void visuHeur( Graphics g , int x , int y )
	{
		double hauteur = (_Map.getMap()[x][y]).getHauteur();
		if (hauteur > 90)
		{
			g.setColor(new Color(51, 0, 0));
		}
		else if (hauteur > 80)
		{
			g.setColor(new Color(153, 0, 0));
		}
		else if (hauteur > 70)
		{
			g.setColor(new Color(0, 102, 0));
		}
		else if (hauteur > 60)
		{
			g.setColor(new Color(0, 153, 0));
		}
		else if (hauteur > 50)
		{
			g.setColor(new Color(102, 255, 255));

		}
		else if (hauteur > 40)
		{
			g.setColor(new Color(51, 204, 255));
		}
		else if (hauteur > 30)
		{
			g.setColor(new Color(51, 102, 255));
		}
		else if (hauteur > 20)
		{

			g.setColor(new Color(51, 51, 255));
		}
		else if (hauteur > 10)
		{
			g.setColor(new Color(0, 0, 153));
		}
	}

	protected void visuKingdom( Graphics g , int x , int y )
	{
		g.setColor(new Color(255, 255, 255));
		TerrainType terrrainType = (_Map.getMap()[x][y]).getTerrainType();
		if (TerrainType.WATER.equals(terrrainType))
		{
			g.setColor(new Color(166, 202, 240));
		}
		else if (_Map.getMap()[x][y].getEmpire() != -1 && _Map.getMap()[x][y].getEmpire() < _colors.size())
		{
			g.setColor(_colors.get(_Map.getMap()[x][y].getEmpire()));
		}
		if (TerrainType.CITY.equals(terrrainType))
		{
			g.setColor(new Color(0, 0, 0));
		}
	}

	protected void visuTemp( Graphics g , int x , int y )
	{
		double temp = (_Map.getMap()[x][y]).getTemperature();
		TerrainType terrrainType = (_Map.getMap()[x][y]).getTerrainType();
		if (TerrainType.WATER.equals(terrrainType) && temp > 5)
		{
			g.setColor(new Color(166, 202, 240));
		}
		else if (TerrainType.WATER.equals(terrrainType) && temp < 5)
		{
			g.setColor(new Color(150, 150, 150));
		}
		else if (temp < 10)
		{
			g.setColor(new Color(255, 255, 255));
		}
		else if (temp < 90)
		{
			g.setColor(new Color(177, 253, 69));
		}
		else
		{
			g.setColor(new Color(239, 212, 86));
		}
	}

	@Override
	public void init( GameContainer pGC ) throws SlickException
	{

		//
		_pos = new Position(100, 100);
		_mode = ModeVisu.RELIEF;
		//
		_Map = new TerrainMap(513);
		_Map.generateWorld();
		// _Map.generateCity();
		_GenerateurKingdom = new GenerateurKingdom();
		_GenerateurKingdom.createKingdom(_Map);
		// _Map.generateEmpire();
		//
		Randomiseur random = new Pseudoalea("color");
		_colors = new ArrayList<Color>();
		for (int i = 0; i < 100; i++)
		{
			_colors.add(new Color((int) (random.random() * 255), (int) (random.random() * 255), (int) (random.random() * 255)));
		}
	}

	@Override
	public void update( GameContainer gc , int arg1 ) throws SlickException
	{
		Input input = gc.getInput();
		if (input.isKeyDown(Input.KEY_Z) && _pos.getY() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX(), _pos.getY() - DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_S) && _pos.getY() + DEPLACEMENT < _Map.getMap().length - NBSCREEN)
		{
			_pos = new Position(_pos.getX(), _pos.getY() + DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_Q) && _pos.getX() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX() - DEPLACEMENT, _pos.getY());
		}
		if (input.isKeyDown(Input.KEY_D) && _pos.getX() + DEPLACEMENT < _Map.getMap().length - NBSCREEN)
		{
			_pos = new Position(_pos.getX() + DEPLACEMENT, _pos.getY());
		}
		if (input.isKeyDown(Input.KEY_1))
		{
			_mode = ModeVisu.ALL;
		}
		if (input.isKeyDown(Input.KEY_2))
		{
			_mode = ModeVisu.RELIEF;
		}
		if (input.isKeyDown(Input.KEY_3))
		{
			_mode = ModeVisu.TEMPERATURE;
		}
		if (input.isKeyDown(Input.KEY_4))
		{
			_mode = ModeVisu.KINGDOM;
		}
		if (input.isKeyDown(Input.KEY_P))
		{
			_GenerateurKingdom.aggrandir(2);
		}

	}
}
